package cn.nkjobsearch.action;

import com.opensymphony.xwork2.ActionSupport;
import cn.nkjobsearch.search.Search;
import cn.nkjobsearch.convert.City;
import cn.nkjobsearch.convert.Experience;
import cn.nkjobsearch.convert.EduLevel;
import cn.nkjobsearch.convert.Category;
import cn.nkjobsearch.convert.Salary;
import cn.nkjobsearch.convert.JobNature;

public class SearchAction extends ActionSupport 
{
	private String keywordList = new String();
	private int jobCategory;
	private String province = new String();
	private String city = new String();
	private int salary;
	private int exp;
	private int pubDate;
	private int edu;	
	private int jobNature;
	private String[][] res = null;
	StringBuilder result = new StringBuilder();
	String[] extendKeyword = null;
	String[] extendCity = null;
	private static final int listNumOfPage = 40;
	private int pageNum;


	public String getKeywordList() {
		return keywordList;
	}
	public void setKeywordList(String keywordList) {
		this.keywordList = keywordList;
	}
	public int getJobCategory() {
		return jobCategory;
	}
	public void setJobCategory(int jobCategory) {
		this.jobCategory = jobCategory;
	}
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	public int getExp() {
		return exp;
	}
	public void setExp(int exp) {
		this.exp = exp;
	}
	public int getPubDate() {
		return pubDate;
	}
	public void setPubDate(int pubDate) {
		this.pubDate = pubDate;
	}
	public int getEdu() {
		return edu;
	}
	public void setEdu(int edu) {
		this.edu = edu;
	}
	public int getJobNature() {
		return jobNature;
	}
	public void setJobNature(int jobNature) {
		this.jobNature = jobNature;
	}
	public String[] getExtendKeyword() {
		return extendKeyword;
	}
	public void setExtendKeyword(String[] extendKeyword) {
		this.extendKeyword = extendKeyword;
	}
	public String[] getExtendCity() {
		return extendCity;
	}
	public void setExtendCity(String[] extendCity) {
		this.extendCity = extendCity;
	}
	public String getPageNum() {
		String pageString = new String(); 
		pageString = "<div id = \"pageId\">共有 "+ this.pageNum +" 页";
		for(int i=1;i<=this.pageNum;i++)
		{
			pageString += "<span onclick=\"show_content("+ i +")\"> ["+ i +"] </span>";
		}
		pageString += "</div>";
		return pageString;
	}
	public String getResult() 
	{
		return result.toString();
	}
	@Override
	public String execute() throws Exception 
	{
		Search search = new Search();
		System.out.println("come to execute()   keywordList: "+keywordList+"extendKeyword: "+
				extendKeyword+" jobCategory: "+jobCategory+" city: "+City.getCityCode(city)
				+" extendCity: "+extendCity+" salary: "+salary +" exp: "+ exp +" pubdate: "+ pubDate  + edu + jobNature );
		this.res = search.go(keywordList,extendKeyword,jobCategory,City.getCityCode(city),extendCity,salary, exp, pubDate, edu, jobNature);
//		this.res = search.go("java软件工程师");
		if(null == res)
		{
			this.result.append("没有数据");
			this.pageNum = 0;
			return SUCCESS;
		}
		System.out.print("res is ok");
		int num = listNumOfPage;
		for(String[] one : res)
		{
			num++;
			if(1 == num%listNumOfPage)
			{
				this.result.append("<div id=\"pagenum"+num/listNumOfPage+"\" class=\"no_show\">");
				this.result.append("<table class=\"tablesorter\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\">");
				this.result.append("<thead><tr><th>序号</th><th>职位ID</th><th>职位名称</th><th>公司名称</th><th>城市</th><th>工作经验</th>");
				this.result.append("<th>发布日期</th><th>学历</th><th>职位分类</th><th>薪水</th><th>职位类别</th><th>来源</th>");
				this.result.append("</tr></thead><tbody>");
			}
			this.result.append("<tr><td>").append(one[0]);
			this.result.append("</td><td>").append(one[1]);
			this.result.append("</td><td><a href="+Salary.getJobUrlViaCode(one[11], one[12], one[6])+" target=\"_blank\">").append(one[2]);
			this.result.append("</a></td><td>").append(Salary.getOrgNameViaCode(one[3]));
			this.result.append("</td><td>").append(City.getCityViaCode(Integer.parseInt(one[4])));
			this.result.append("</td><td>").append(Experience.getExpViaCode(Integer.parseInt(one[5])));
			this.result.append("</td><td>").append(one[6]);
			this.result.append("</td><td>").append(EduLevel.getEduViaCode(Integer.parseInt(one[7])));
			this.result.append("</td><td>").append(Category.getCategoryViaCode(Integer.parseInt(one[8])));
			this.result.append("</td><td>").append(Salary.getSalaryViaCode(one[9]));
			this.result.append("</td><td>").append(JobNature.getJobNatureViaCode(Integer.parseInt(one[10])));
			this.result.append("</td><td>").append(Salary.getSiteViaCode(one[11]));
			this.result.append("</td></tr>");
			if(0 == num%listNumOfPage)
			{
				this.result.append("</tbody></table></div>");
			}
		}
		if(0 != num%listNumOfPage)
		{
			this.result.append("</tbody></table></div>");
		}
		this.pageNum = num/listNumOfPage;
		return SUCCESS;
	}
	@Override
	public void validate() 
	{
		if(null == this.keywordList || "".equals(this.keywordList.trim()))
		{
			System.out.print("validate is working");
			this.addFieldError("keywordList", "error");
		}
	}
	
}
